Using Xbasic to Create Queries
Description
The RADIO2 option button asks the user whether to preview or print, and stores the result into a form variable named print_option. BUTTON1 executes an Xbasic routine that examines all these variables and calls the appropriate preview or print functions.
Xbasic Code Run by the Button1 OnPush Event
if (option = 1) then filter = "date = [varD->today]" else if (option = 2) then filter = "between(date, [varD->begindate], [varD->enddate])" else if (option = 3) then list_invno = stritran(invoice_numbers,",",crlf()) list_invo = alltrim(list_invno) filter = *for_each(x, "invoice_number = " + quote(x) ,list_invo) filter = alltrim(filter) filter = stritran(filter, crlf(), " .or. ") else if (option = 4) then filter = "between(invoice_number, [varC->begin_inv], [varC->end_inv])" end if filter = replace_parameters(filter, local_variables()) if (print_option = "preview") then report.preview("Invoice", filter) else report.print("invoice", filter) end if
An Explanation of the Xbasic Code
The RADIO1 option buttons set the value of a form variable named option. The value 1 corresponds to the "Today's Invoices" selection. If option is equal to 1, we set the filter variable equal to the expression "date = [varD->today]", where today is a field initialized to the current date.
if (option = 1) then filter = "date = [varD->today]"
The "Range of Dates" selection corresponds to option equal to 2. In this case we set filter equal to the expression "between(date, [varD->begindate], [varD->enddate])". The between() function returns true if the first argument, date, is between the values of the second and third arguments, the begindate and enddate fields.
else if (option = 2) then filter = "between(date, [varD->begindate], [varD->enddate])"
The "Specific Invoice Number(s)" selection corresponds to option equal to 3. The stritran() function replaces each comma in the invoice_numbers field with the carriage return and line feed characters. The result is saved into the list_invno character variable.
- Before
- After
- A001, A002, A003
A001 A002 A003
else if (option = 3) then list_invno = stritran(invoice_numbers,",",crlf())
The alltrim() function removes any leading or trailing spaces.
list_invo = alltrim(list_invno)
The *for_each() function adds "invoice_number = " to each line, places quotes around the invoice number, and saves the results into the filter variable.
- Before
- After
- A001
invoice_number = "A001"
- A002
invoice_number = "A002"
- A003
invoice_number = "A003"
filter = *for_each(x, "invoice_number = " + quote(x) ,list_invo)
The alltrim() function again removes any leading or trailing spaces.
filter = alltrim(filter)
The stritran()function again reformats the data to substitute ".or." for each pair of carriage return and line feed characters.
- Before
- After
- invoice_number = "A001" invoice_number = "A002" invoice_number = "A003"
invoice_number = "A001" .or. invoice_number = "A002" .or. invoice_number = "A003"
filter = stritran(filter, crlf(), " .or. ")
The "Range of Invoice Numbers" selection corresponds to option equal to 4. The between() function returns true if an invoice number is between the begin_inv and end_inv field values.
else if (option = 4) then filter = "between(invoice_number, [varC->begin_inv], [varC->end_inv])" end if
The replace_parameters() function inserts the actual values of the referenced variables into the filter expression. The reason that this function is necessary is that the variables only have local session scope and cannot be resolved by the preview() and print() functions. An alternative solution that would not require the use of replace_parameters would be to first declare the variables as shared.
filter = replace_parameters(filter, local_variables())
Finally, we call the appropriate function depending on the value of the print_option variable.
if (print_option = "preview") then report.preview("Invoice", filter) else report.print("invoice", filter) end if
See Also